﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Jsons;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Framework.Verifications;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Weblications.Sango.Environment;
using Pixysoft.Web;
using Pixysoft.Weblications.Sango.Command.general;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Weblications.Sango.Protocol;
using Pixysoft.Weblications.Sango.Protocol.player;

namespace Pixysoft.Weblications.Sango.Command.player
{
    partial class SangoPlayerHandler
    {
        public player_scout player_scout()
        {
            string userCode = SangoManager.Authentication();

            player_scout response = ReflectionManager.CreatePojo<player_scout>();
            response.Succeed = false;

            Entities.Usr_Profile profile = UsrProfileDao.getUsrProfile(userCode);
            Entities.Usr_Activity history = UsrActivityDao.getHistory(userCode);

            if (history.Scoutcount >= PlayerFormular.default_scoutcount)
            {
                response.Message = "今天征收次数已经用完";
                return response;
            }

            Entities.Usr_Queue queue = UsrQueueDao.UsrQueueStartup(
                userCode, CtiQueueTypecodeCollection.SCOUT, userCode, PlayerFormular.createScoutCooldownTimespan(history));
            if (queue == null)
            {
                response.Message = "征收冷却中";
                return response;
            }


            // party addition

            Entities.Usr_Building houseBuilding = UsrBuildingDao.getUserBuilding(userCode, CtiBuildingTypecodeCollection.HOUSE);
            long cashvalue = PlayerFormular.getScoutValue(profile.Userlevel, houseBuilding.Buildinglevel);
            Entities.Usr_Partymember partyMember = PartyDao.tryGetActiveMember(userCode);
            if (partyMember != null)
            {
                cashvalue = PartyDao.getMissionAddition(partyMember.Partyprimarycode, PtyMissionTypecodeCollection.SCOUT, cashvalue);
            }
            if (!UsrProfileDao.tryIncreaseCashvalue(profile, cashvalue))
            {
                response.Message = "银币无法继续增加";
                return response;
            }

            // 征收掉落金币

            bool dropGold = PlayerFormular.dropScoutGoldvalue(profile.Userlevel);
            UsrProfileDao.increaseGoldvalue(profile, PlayerFormular.default_scout_dropgold);

            Entities.Usr_Mission mission = MissionDao.getMission(userCode);

            // database

            IEntityNoebeTransaction command = SettingHelper.EntityTransaction;

            command.Begin(delegate()
            {
                command.Update(queue);

                // history

                history.Scoutcount = (history.Scoutcount + 1);
                command.Update(history);

                command.Update(profile);

                if (MissionDao.processDailyMission(mission, MissionDailyCollection.SCOUT))
                {
                    command.Update(mission);
                }

                command.Commit();
            });


            response.queue = (UsrQueueFacade.ToQueue(queue));
            if (dropGold)
                response.goldcoin = (PlayerFormular.default_scout_dropgold);
            else
                response.goldcoin = (0);
            response.activity = UsrProfileFacade.ToActivity(profile, history);
            response.profile = UsrProfileFacade.ToProfile(profile);
            response.cashvalue = cashvalue;
            response.scoutvalue = PlayerFormular.getScoutValue(profile.Userlevel, houseBuilding.Buildinglevel);
            response.Succeed = true;
            return response;
        }
    }

    public interface player_scout : ISangoResponse
    {
        player_queue queue { get;set;}

        player_activity activity { get;set;}
        player_profile profile { get;set;}

        long goldcoin { get;set;}
        long cashvalue { get;set;}

        /// <summary>
        /// 可征银币数量
        /// </summary>
        long scoutvalue { get;set;}
        /// <summary>
        /// 强征价格
        /// </summary>
        long forcescoutprice { get;set;}
    }
}
